/**
 * Copyright 2010 Jake Jackson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jake.nb.ext.ant.hints.impl.dependserrors;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.jake.nb.ext.ant.hints.AntHint;
import org.jake.nb.ext.ant.hints.Configuration;
import org.jake.nb.ext.ant.hints.Paths;
import org.jake.nb.ext.ant.hints.SpanContainer;
import org.jake.nb.ext.ant.parser.Span;
import org.jake.nb.ext.ant.parser.Spans;
import org.netbeans.modules.editor.NbEditorUtilities;
import org.netbeans.spi.editor.hints.ErrorDescription;
import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
import org.netbeans.spi.editor.hints.Fix;
import org.netbeans.spi.editor.hints.Severity;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;

/**
 * An anthint checking the depends clause is not empty
 * @author JakeJ
 */
@ServiceProvider(service=AntHint.class, path="org-jake-nb-ext-ant-hints")
public class EmptyDepends implements AntHint {

  private Configuration configuration;

  public EmptyDepends() {
    configuration = new Configuration(EmptyDepends.class.getCanonicalName(),
            "Empty depends",
            "Indicates the depends clause does not contain any targets to call. This is not wrong at all"
            + ", but somewhat useless.",
            true,
            Severity.HINT, Paths.DEPENDS);
  }

  @Override
  public Collection<ErrorDescription> getErrors(final SpanContainer spanContainer) {
    final Collection<ErrorDescription> errors = new HashSet<ErrorDescription>();
    final Spans dependSpans = spanContainer.getDependSpans();
    for (final Span span : dependSpans.getSpans()) {
      if(getDepends(span).trim().length() <= 0) {
        final Document document = spanContainer.getDocument();
        errors.add(ErrorDescriptionFactory.createErrorDescription(configuration.getSeverity(),  NbBundle.getMessage(this.getClass(), "DESC_ERR_EMPTYDEPENDS"), // NOI18N
                Collections.<Fix>singletonList(new RemoveDependsFix(document, span)),
                document, NbEditorUtilities.getLine(document, span.getStart(), true).getLineNumber() + 1)); // NOI18N
      }
    }
    return errors;
  }

  @Override
  public Configuration getConfiguration() {
    return configuration;
  }

  /**
   * Gets the full string of the depends attribute, e.g. depends="a, b", but only in case there are multiple entries
   * @param span
   * @return
   */
  private String getDepends(final Span span) {
    String text = null;
    try {
      text = span.getDocument().getText(span.getStart(), span.getLength() + 1);
    } catch (BadLocationException ex) {
      Exceptions.printStackTrace(ex);
      text = ""; // NOI18N
    }

    if(text.length() > 0) {
      final int firstQuote = text.indexOf('"'); // NOI18N
      final int lastQuote = text.indexOf('"', firstQuote + 1); // NOI18N
      text = text.substring(firstQuote + 1, lastQuote);
    }

    return text;
  }
}
